package br.pucrio.inf.les.SPLAnalyser.impl.query.experimentos.v6_mm5_v7_mm6;

import java.util.ArrayList;
import java.util.List;

import br.pucrio.inf.les.SPLAnalyser.core.Query;
import br.pucrio.inf.les.SPLAnalyser.core.models.impactanalysismodel.Asset;
import br.pucrio.inf.les.SPLAnalyser.core.models.impactanalysismodel.Class;
import br.pucrio.inf.les.SPLAnalyser.core.models.impactanalysismodel.CodePiece;
import br.pucrio.inf.les.SPLAnalyser.core.models.impactanalysismodel.Feature;
import br.pucrio.inf.les.SPLAnalyser.core.models.impactanalysismodel.Field;
import br.pucrio.inf.les.SPLAnalyser.core.models.impactanalysismodel.ImpactAnalysisModel;
import br.pucrio.inf.les.SPLAnalyser.core.models.impactanalysismodel.Mapping;
import br.pucrio.inf.les.SPLAnalyser.core.models.impactanalysismodel.Method;
import br.pucrio.inf.les.SPLAnalyser.core.models.util.ImpactAnalysisModelContent;
import br.pucrio.inf.les.SPLAnalyser.core.models.util.ModelContent;

//Retorna todas as entidades que dependem diretamente da feature.
public class Rastreabilidadev6_v7 implements Query {

	private ImpactAnalysisModelContent analysisModelContent;
	private ModelContent queryModelContent;
	private ImpactAnalysisModel queryModel;
	private String featureName1;
	private String featureName2;
	
	
	public Rastreabilidadev6_v7(String featureName1, String featureName2){
		this.featureName1 = featureName1;
		this.featureName2 = featureName2;
	}
	
	public ImpactAnalysisModel runQuery(ImpactAnalysisModel analysisModel) {
		analysisModelContent = new ImpactAnalysisModelContent(analysisModel);
		queryModelContent = new ModelContent();
		query();
		queryModel = queryModelContent.getAnalysisModel();
		return queryModel;
	}

	private void query() {
		Feature feature1 = null;
		Feature feature2 = null;
		List<Asset> assets = new ArrayList<Asset>();
		int classCount = 0;
		int methodCount = 0;
		int fieldCount = 0;
		int codePieceCount = 0;
		
		feature1 = analysisModelContent.findFeature(featureName1);
		feature2 = analysisModelContent.findFeature(featureName2);
		
		List<Mapping> mappings = analysisModelContent.findMappingOf(feature1);
		mappings.addAll(analysisModelContent.findMappingOf(feature2));
		for(Mapping map : mappings){
			boolean test = true;
			for(Asset asset : assets){
				if(asset.getName().equals(map.getAssets().getName())
						&& asset.getPath().equals(map.getAssets().getPath()))
					test = false;
			}
			if(test) assets.add(map.getAssets());		
		}
		
		queryModelContent.addAsset(assets);
		
		
		
		
		for(Asset asset : assets){
			if(asset instanceof Class){
				classCount++;
			} else if(asset instanceof Method){
				methodCount++;
			} else if(asset instanceof Field){
				
				fieldCount++;
			} else if(asset instanceof CodePiece){
				codePieceCount++;
			}
		}
		System.out.println("Total      : "+ assets.size());
		System.out.println("Classe     : "+ classCount);
		System.out.println("Metodos    : "+ methodCount);
		System.out.println("Atributos  : "+ fieldCount);
		System.out.println("Fragmentos : "+ codePieceCount);
		
		
	}

	public String getName() {
		return this.getClass().getSimpleName();
	}
}
